package com.jml.蚂蚁.netty.普通client;

import io.netty.channel.Channel;
import java.net.InetSocketAddress;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
 * @author:jml
 * @date: 2019/12/24 10:57 @description：
 */
public class NettyClient {
	
    public static void main(String[] args) {
        // 创建nioEventLoopGroup
        NioEventLoopGroup group = new NioEventLoopGroup();
        Bootstrap bootstrap = new Bootstrap();
	    bootstrap = bootstrap.group(group);
	    //NioSocketChannel表示这是个客户端
	    bootstrap = bootstrap.channel(NioSocketChannel.class);
	    //使用Socket套接字设置远程连接地址
	    bootstrap = bootstrap.remoteAddress(new InetSocketAddress("127.0.0.1", 8080));
	    //
		bootstrap = bootstrap.handler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(new ClientHandler());
                }
            });
        try {
            // 建立同步连接
            ChannelFuture channelFuture = bootstrap.connect();
	        channelFuture = channelFuture.sync();
	
	        Channel channel = channelFuture.channel();
	        ChannelFuture channelFuture001 = channel.closeFuture();
	        channelFuture001.sync();
	        
        } catch (Exception e) {

        } finally {
            group.shutdownGracefully();
        }

    }
}
